package com.tx.mq.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    private static final String QUEUE_NAME = "test";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("localhost");

        Connection connection = cf.newConnection();
        Channel channel = connection.createChannel();
        String queue = channel.queueDeclare().getQueue();
        channel.basicQos(1);
        channel.exchangeDeclare("bbb", BuiltinExchangeType.TOPIC);
        channel.queueBind(queue, "bbb", "bbb.ccc");
        System.out.println("[*] consumer waiting for message. to exit press Ctrl+c");

        DeliverCallback callback = ((consumerTag, delivery) -> {
            String msg = new String(delivery.getBody(), "utf-8");
            System.out.println(" [x] Received '" + msg + "'");
            channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, false);
        });

        channel.basicConsume(queue, false, callback, consumerTag -> {});
    }
}
